<?php

namespace Admin\Pages;

/**
 * Description of Index
 *
 * @author nofear
 */
class PageForm extends Page {

    private $entityNamespace = 'Shared\Main\Page';
    private $entity = '\Shared\Main\Page';

    public function index($id = null) {
        $this->view = new \System\Template('@admin/pageForm.twig');
        $this->view->adminMode = $this->request->get('adminMode');
        $id = $this->sanitize($id, $type = 'int');

        $Session = $this->getSession();
        if ($Session->have('message')) {
            $this->view->message = $Session->getMessage();
            $Session->removeMessage();
        }

        if ($id) {
            $Object = $this->EntityManager->find($this->entityNamespace, $id);

            if (!$Object) {
                $this->createNotFound('Няма такава страница.', 500);
            }
        } else {
            $Object = new $this->entity;
        }

        $this->view->form = new \Admin\Forms\Page($Object);
    }

    public function save() {
        $Saver = new \Admin\Savers\Saver();
        $request = $this->getRequest();
        $id = $this->sanitize($request->get('id'));
        $title = $this->sanitize($request->get('title'));
        $key = $this->sanitize($request->get('key'));
        $content = $request->get('content');

        if (
                $title
        ) {
            if ($id) {
                $Object = $this->EntityManager->find($this->entityNamespace, $id);

                if (!$Object) {
                    $this->createNotFound('Такава страница не съществува.');
                }
                
                $MainPicture = $this->EntityManager->getRepository('Shared\Main\SiteObject')->getMainPicture($Object, $force = true);

                if ($MainPicture) {
//                    $MainPicture->getSiteObjectGalleryItem()->setIsActive($request->get('giActive'));
                    $MainPicture->getSiteObjectGalleryItem()->setIsActive(true);
                    $this->EntityManager->persist($MainPicture);
                }
            } else {
                $Object = new \Shared\Main\Page();
            }
            
            $this->LRCSupport->getManager($Object)->set($this->LRCSupport->getCurrentLanguage(), 'title', $title);
            $this->LRCSupport->getManager($Object)->set($this->LRCSupport->getCurrentLanguage(), 'content', $content);
//            $Object->setTitle($title);
            if ($key) {
                $Object->setKey($key);
            }
//            $Object->setContent($content);
            $Saver->process($Object);
            $Saver->saveMultimedia($Object);
            
            $Metadata = $Saver->saveMetadata($Object);
            $Object->setMetaData($Metadata);
            $Saver->commit($Object);
        } else {
            $this->redirect(\Shared\URLManager::getInstance()->createPath('admin_pages'));
        }

        $message['text'] = 'Промените са запазени успешно.';
        $message['type'] = 'alert-success';

        $Session = $this->getSession();
        $Session->setMessage($message);

        $this->redirect(\Shared\URLManager::getInstance()->createPath('admin_page_edit', array('id' => $Object->getId())));
    }

    public function delete($id) {
        $id = $this->sanitize($id);
        $Session = $this->getSession();

        $message['text'] = 'Възникна грешка!';
        $message['type'] = 'alert-danger';

        if ($id) {
            $Product = $this->EntityManager->find($this->entityNamespace, $id);

            if ($Product) {
                $this->EntityManager->remove($Product);
                $this->EntityManager->flush();

                $message['text'] = 'Страницата е изтрита успешно.';
                $message['type'] = 'alert-success';
            }
        }

        $Session->setMessage($message);
        $this->redirect(\Shared\URLManager::getInstance()->createPath('admin_pages'));
    }

}
